//
//  QZHTabBarController.m
//  LiveStar
//
//  Created by 漪珊 on 2016/12/8.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "QZHTabBarController.h"
#import "LoginViewController.h"
#import "StarMyViewController.h"
#import "FansMyViewController.h"
#import "StartListViewController.h"
#import "RegisterViewController.h"
#import "QZHNavgationController.h"
#import "HomeStarViewController.h"
#import "HomefansViewController.h"
#import "FindViewController.h"
#import "FansTrendViewController.h"
#import "StarTrendViewController.h"
#import "LivingViewController.h"
#import "HomefansVideoViewController.h"


@interface QZHTabBarController ()

@end

@implementation QZHTabBarController
+ (void)initialize {
    
    // 设置为不透明
    [[UITabBar appearance] setTranslucent:YES];
    // 设置背景颜色
//    [UITabBar appearance].barTintColor = [UIColor colorWithRed:255.0 green:255.0 blue:255.0 alpha:0.1];
    
    // 拿到整个导航控制器的外观
    UITabBarItem * item = [UITabBarItem appearance];
    item.titlePositionAdjustment = UIOffsetMake(0, 1.5);
    
    // 普通状态
    NSMutableDictionary * normalAtts = [NSMutableDictionary dictionary];
    normalAtts[NSFontAttributeName] = [UIFont systemFontOfSize:12];
    normalAtts[NSForegroundColorAttributeName] = [UIColor grayColor];
    [item setTitleTextAttributes:normalAtts forState:UIControlStateNormal];
    
    // 选中状态
    NSMutableDictionary *selectAtts = [NSMutableDictionary dictionary];
    selectAtts[NSFontAttributeName] = [UIFont systemFontOfSize:12];
    selectAtts[NSForegroundColorAttributeName] = [UIColor mainColor];
    [item setTitleTextAttributes:selectAtts forState:UIControlStateSelected];
}

- (instancetype)initWithUserType:(NSString *)userType {
    self.userType = userType;
    if (self = [super init]) {
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self CreatControllersWithUserType:self.userType];
    // Do any additional setup after loading the view.
}

#pragma mark -- 创建子控制器
- (void)CreatControllersWithUserType:(NSString *)userType {
    if ([userType isEqualToString:@"100"]) {//明星
        [self addChildViewControllerWithClassname:[HomeStarViewController description] imagename:@"Home" title:@"首页"];
        [self addChildViewControllerWithClassname:[StarTrendViewController description] imagename:@"satarZoom" title:@"空间"];
        [self addChildViewControllerWithClassname:[StarMyViewController description]imagename:@"My" title:@"我"];
    } else {//粉丝
        [self addChildViewControllerWithClassname:[HomefansViewController description] imagename:@"Home" title:@"首页"];
        [self addChildViewControllerWithClassname:[FansTrendViewController description] imagename:@"StarFriend" title:@"星友圈"];
        [self addChildViewControllerWithClassname:[HomefansVideoViewController description] imagename:@"Starplace" title:@"星部落"];
        [self addChildViewControllerWithClassname:[FansMyViewController description]imagename:@"My" title:@"我"];
    }
    
    
//    NHServiceListRequest *request = [NHServiceListRequest nh_request];
//    request.nh_url = kNHHomeServiceListAPI;
//    [request nh_sendRequestWithCompletion:^(id response, BOOL success, NSString *message) {
//        if (success) {
//            NHBaseNavigationViewController *homeNav = (NHBaseNavigationViewController *)self.viewControllers.firstObject;
//            NHHomeViewController *home = (NHHomeViewController *)homeNav.viewControllers.firstObject;
//            home.models = [NHServiceListModel modelArrayWithDictArray:response];
//        }
//    }];

}

// 添加子控制器
- (void)addChildViewControllerWithClassname:(NSString *)classname
                                  imagename:(NSString *)imagename
                                      title:(NSString *)title {
    
    UIViewController *vc = [[NSClassFromString(classname) alloc] init];
    QZHNavgationController *nav = [[QZHNavgationController alloc] initWithRootViewController:vc];
    nav.userType = self.userType;
    // 设置导航栏背景颜色
    if ([self.userType isEqualToString:@"100"]||self.userType == nil) {
        nav.navigationBar.barTintColor = [UIColor navagationBarColor];
        // 设置导航栏标题文字颜色
        // 创建字典保存文字大小和颜色
        NSMutableDictionary * color = [NSMutableDictionary dictionary];
        color[NSFontAttributeName] = [UIFont systemFontOfSize:17];
        color[NSForegroundColorAttributeName] = [UIColor whiteColor];
        [[UINavigationBar appearance] setTitleTextAttributes:color];
        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    } else {
        nav.navigationBar.barTintColor = [UIColor navagationBarColor];
        // 设置导航栏标题文字颜色
        // 创建字典保存文字大小和颜色
        NSMutableDictionary * color = [NSMutableDictionary dictionary];
        color[NSFontAttributeName] = [UIFont systemFontOfSize:17];
        color[NSForegroundColorAttributeName] = [UIColor whiteColor];
        [[UINavigationBar appearance] setTitleTextAttributes:color];
    }

    ;
    nav.tabBarItem.title = title;
    nav.tabBarItem.image = [UIImage imageNamed:imagename];
    nav.tabBarItem.selectedImage = [[UIImage imageNamed:[imagename stringByAppendingString:@"_press"]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [self addChildViewController:nav];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
